// pages/teacher/detail.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    teacherId: '',
    teacherInfo: {},
    teacherCourses: [],
    teacherReviews: [],
    isFollowed: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.id) {
      this.setData({
        teacherId: options.id
      });
      
      this.loadTeacherInfo();
      this.loadTeacherCourses();
      this.loadTeacherReviews();
      this.checkFollowStatus();
    } else {
      wx.showToast({
        title: '讲师ID不存在',
        icon: 'none'
      });
      
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: `${this.data.teacherInfo.name} - 知汇堂讲师`,
      path: `/pages/teacher/detail?id=${this.data.teacherId}`
    };
  },

  /**
   * 加载讲师信息
   */
  loadTeacherInfo() {
    // 模拟API请求
    setTimeout(() => {
      // 生成模拟数据
      const teacherInfo = {
        id: this.data.teacherId,
        name: '张教授',
        avatarUrl: '/assets/images/teacher/avatar1.png',
        title: '资深前端开发工程师',
        isVerified: true,
        tags: ['前端', '热门', 'JavaScript'],
        courseCount: 15,
        studentCount: 8642,
        rating: '4.9',
        introduction: '张教授，资深前端开发工程师，拥有10年以上的前端开发经验。曾就职于多家知名互联网公司，参与过多个大型项目的开发。精通HTML、CSS、JavaScript、Vue、React等前端技术，对前端工程化、性能优化有深入研究。\n\n授课风格通俗易懂，注重实战，善于将复杂的技术概念简单化，帮助学员快速掌握前端开发技能。已出版《前端开发实战指南》等多本技术书籍，在业内享有很高声誉。'
      };
      
      this.setData({
        teacherInfo
      });
    }, 500);
  },

  /**
   * 加载讲师课程
   */
  loadTeacherCourses() {
    // 模拟API请求
    setTimeout(() => {
      // 生成模拟数据
      const courses = [];
      
      for (let i = 0; i < 3; i++) {
        courses.push({
          id: 'C' + (1000 + i),
          title: ['Vue.js 3.0从入门到精通', 'React 18实战教程', 'JavaScript高级程序设计'][i],
          coverUrl: `/assets/images/course/course${i + 1}.png`,
          price: (199 + i * 50).toFixed(2),
          studentCount: 1000 + i * 500
        });
      }
      
      this.setData({
        teacherCourses: courses
      });
    }, 600);
  },

  /**
   * 加载讲师评价
   */
  loadTeacherReviews() {
    // 模拟API请求
    setTimeout(() => {
      // 生成模拟数据
      const reviews = [];
      
      const reviewContents = [
        '老师讲解非常清晰，课程内容丰富，实用性强，收获很大！',
        '课程质量很高，老师经验丰富，讲解通俗易懂，非常推荐！',
        '老师很负责任，遇到问题都能及时解答，学习体验很好。'
      ];
      
      for (let i = 0; i < 3; i++) {
        reviews.push({
          id: 'R' + (1000 + i),
          userName: ['李同学', '王同学', '张同学'][i],
          avatarUrl: `/assets/images/avatar/user${i + 1}.png`,
          rating: (4.5 + i * 0.2).toFixed(1),
          content: reviewContents[i],
          time: '2023-05-' + (10 + i)
        });
      }
      
      this.setData({
        teacherReviews: reviews
      });
    }, 700);
  },

  /**
   * 检查关注状态
   */
  checkFollowStatus() {
    // 从本地存储获取关注状态
    const followedTeachers = wx.getStorageSync('followedTeachers') || [];
    const isFollowed = followedTeachers.includes(this.data.teacherId);
    
    this.setData({ isFollowed });
  },

  /**
   * 关注/取消关注讲师
   */
  toggleFollow() {
    const isFollowed = this.data.isFollowed;
    const newStatus = !isFollowed;
    
    // 更新本地状态
    this.setData({ isFollowed: newStatus });
    
    // 更新存储
    let followedTeachers = wx.getStorageSync('followedTeachers') || [];
    
    if (newStatus) {
      // 添加关注
      if (!followedTeachers.includes(this.data.teacherId)) {
        followedTeachers.push(this.data.teacherId);
      }
      wx.showToast({
        title: '已关注',
        icon: 'success'
      });
    } else {
      // 取消关注
      followedTeachers = followedTeachers.filter(id => id !== this.data.teacherId);
      wx.showToast({
        title: '已取消关注',
        icon: 'success'
      });
    }
    
    wx.setStorageSync('followedTeachers', followedTeachers);
    
    // 标记需要刷新列表页
    wx.setStorageSync('refreshTeacherList', true);
  },

  /**
   * 查看课程详情
   */
  viewCourseDetail(e) {
    const courseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/course/detail?id=${courseId}`
    });
  },
  /**
   * 查看更多课程
   */
  viewMoreCourses() {
    wx.navigateTo({
      url: `/pages/course/list?teacherId=${this.data.teacherId}`
    });
  },
  
  /**
   * 查看更多评价
   */
  viewMoreReviews() {
    wx.navigateTo({
      url: `/pages/teacher/reviews?id=${this.data.teacherId}`
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.loadTeacherInfo();
    this.loadTeacherCourses();
    this.loadTeacherReviews();
    this.checkFollowStatus();
    
    wx.stopPullDownRefresh();
  }
})